'use strict';

const { formatDate } = require("../../utils/format");
module.exports = app => {
  const { INTEGER, DATE } = app.Sequelize;

  const Cart = app.model.define('Cart', {
    id: { type: INTEGER, primaryKey: true, autoIncrement: true },
    type: INTEGER,
    member_user_id: INTEGER, // 用户ID
    product_id: INTEGER, // 商品ID
    product_item_id: INTEGER, // 商品规格ID
    quantity: { type: INTEGER, defaultValue: 1 }, // 商品数量
    selected: { type: INTEGER, defaultValue: 1 }, // 是否选中：0-未选中，1-已选中
    create_time: {
      type: DATE,
      defaultValue: new Date(),
      get() {
        return formatDate(this.getDataValue('create_time'))
      }
    },
    update_time: {
      type: DATE,
      defaultValue: new Date(),
      get() {
        return formatDate(this.getDataValue('update_time'))
      }
    },
  },{
    tableName: 'cart',
    timestamps: true, // 启用时间戳(createdAt, updatedAt)
    updatedAt: 'update_time', // 不想要 updatedAt
    createdAt: 'create_time', // 想要  createdAt 但是希望名称叫做 create_time
  });

  Cart.associate = function() {
    app.model.Cart.belongsTo(app.model.MemberUser, { foreignKey: 'member_user_id', as: 'user' });
    app.model.Cart.belongsTo(app.model.Product, { foreignKey: 'product_id', as: 'product' });
    app.model.Cart.belongsTo(app.model.ProductItem, { foreignKey: 'product_item_id', as: 'productItem' });
  };

  return Cart;
};
